export const useToast = () => {
  const showToast = (message: string, type: 'success' | 'error' | 'warning' | 'info' = 'info') => {
    // 创建toast容器（如果不存在）
    let toastContainer = document.getElementById('toast-container')
    if (!toastContainer) {
      toastContainer = document.createElement('div')
      toastContainer.id = 'toast-container'
      toastContainer.className = 'toast toast-top toast-end z-50'
      document.body.appendChild(toastContainer)
    }

    // 创建toast元素
    const toastElement = document.createElement('div')
    const alertClass = {
      success: 'alert-success',
      error: 'alert-error',
      warning: 'alert-warning',
      info: 'alert-info'
    }[type]

    toastElement.className = `alert ${alertClass} mb-2`
    toastElement.innerHTML = `
      <span>${message}</span>
    `

    // 添加到容器
    toastContainer.appendChild(toastElement)

    // 3秒后自动移除
    setTimeout(() => {
      if (toastElement.parentNode) {
        toastElement.remove()
      }
      // 如果容器为空，也移除容器
      if (toastContainer && toastContainer.children.length === 0) {
        toastContainer.remove()
      }
    }, 3000)
  }

  const showSuccess = (message: string) => {
    showToast(message, 'success')
  }

  const showError = (message: string) => {
    showToast(message, 'error')
  }

  const showWarning = (message: string) => {
    showToast(message, 'warning')
  }

  const showInfo = (message: string) => {
    showToast(message, 'info')
  }

  return {
    showToast,
    showSuccess,
    showError,
    showWarning,
    showInfo
  }
}